/*
 * Copyright 2009, Eniro Norge AS ( http://www.eniro.no)
 *
 * This software is the proprietary information of Eniro Norge AS.
 * Use is subject to license terms.
 */
package com.google.code.ei4j;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;

/**
 * Class for converting images from the command line.
 * <p/>
 * Usage: java Converter [filename] [width] [height] [keep aspect] [outputformat] [outputfilename]
 * <br/>
 * [filename] the file to read: required
 * <br/>
 * [width] target width: required
 * <br/>
 * [height] target height: required
 * <br/>
 * [keep aspect] keep the aspect of the original image: optional, defaults to false
 * <br/>
 * [outputformat] format to convert to: optional, defaults to png
 * <br/>
 * [outputfilename] filename to write result to: optional, defaults to [filename] + . [outputformat]
 * <p/>
 * To list all the formats that are supported use the --listformats parameter
 */
public class Converter {
    private String filename;
    private int width;
    private int height;
    private boolean keepaspect;
    private String outputformat;
    private String outputfilename;

    public Converter(String filename, int width, int height, boolean keepaspect,
                     String outputformat, String outputfilename) {
        this.filename = filename;
        this.width = width;
        this.height = height;
        this.keepaspect = keepaspect;
        this.outputformat = outputformat;
        this.outputfilename = outputfilename;
    }

    public void convert() throws IOException {
        FileOutputStream outputStream = new FileOutputStream(outputfilename);
        FileInputStream inputStream = new FileInputStream(filename);
        try {
            EasyImage.fromStream(inputStream).scaleQuality(width, height, keepaspect)
                    .toFormat(outputformat, outputStream);
        } finally {
            try {
                inputStream.close();
            } finally {
                outputStream.close();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        if (args.length == 1 && "--listformats".equals(args[0])) {
            Collection<String> formats = EasyImage.availableFormats();
            System.out.println("Available formats:");
            for (String format : formats) {
                System.out.println(format);
            }
            System.exit(0);
        }
        if (args.length < 3) {
            System.out.println("Atleast file name, width and height must be given.");
            System.exit(1);
        }
        String filename = args[0];
        int width = Integer.parseInt(args[1]);
        int height = Integer.parseInt(args[2]);
        boolean keepaspect = args.length > 3 && Boolean.parseBoolean(args[3]);
        String outputformat = args.length > 4 ? args[4] : "png";
        String outputfilename = args.length > 5 ? args[5] : filename + "." + outputformat;
        new Converter(filename, width, height, keepaspect, outputformat, outputfilename).convert();
    }
}
